﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminSEOController : Controller
    {
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                SeoModel model = new SeoModel();

                foreach (var seo in db.EntitySEOs)
                {
                    model.listSeo.Add(new Seo()
                    {
                        Id = seo.Id,
                        Titletag = seo.Titletag,
                        Meta_description = seo.Meta_description,
                        Meta_keyword = seo.Meta_keyword,
                        Type = seo.Type
                    });
                }
                return View(model);
            }
        }
        #endregion //index

        #region "Edit"
        public ActionResult Edit(int idSeo)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                Seo model = new Seo();
                EntitySEO seo = db.EntitySEOs.FirstOrDefault(o => o.Id == idSeo);

                if (seo != null)
                {
                    model.Id = seo.Id;
                    model.Titletag = seo.Titletag;
                    model.Meta_description = seo.Meta_description;
                    model.Meta_keyword = seo.Meta_keyword;
                    model.Type = seo.Type;

                    return PartialView(model);
                }
                return PartialView();
            }
           
        }

        [HttpPost]
        public JsonResult GetDetail(int idSeo)
        {
            Seo model = new Seo();
            model.Id = -1;

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntitySEO seo = db.EntitySEOs.FirstOrDefault(o => o.Id == idSeo);

                if (seo != null)
                {
                    model.Id = seo.Id;
                    model.Titletag = seo.Titletag;
                    model.Meta_description = seo.Meta_description;
                    model.Meta_keyword = seo.Meta_keyword;
                    model.Type = seo.Type;
                }
                return Json(model);
            }

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int idSeo, Seo model)
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                //check Id 
                EntitySEO seo = db.EntitySEOs.Where(p => p.Id == idSeo).FirstOrDefault();
                seo.Titletag = model.Titletag;
                seo.Meta_description = model.Meta_description;
                seo.Meta_keyword = model.Meta_keyword;

                db.SubmitChanges();
                return RedirectToAction("Index", "AdminSEO");
            }
        }

        #endregion //edit
    }
}
